export default {
  name: 'money',
  filter: function (val) {
    if (val == null) {
      return '暂无'
    }
    if (isNaN(val)) {
      val = '0'
    }
    let isNegative = false
    if (val.toString().indexOf('-') !== -1) {
      isNegative = true
      val = val.toString().replace('-', '')
    }
    let scale = 2
    val = (Math.round(val * Math.pow(10, scale)) / Math.pow(10, scale)).toFixed(
      scale
    ) // 四舍五入保留两位小数
    val = String(val)
    let dot = val.indexOf('.')
    let decimal = '.00'
    if (dot !== -1) {
      decimal = val.substr(dot)
      val = val.substring(0, dot)
    }
    if (val.length > 3) {
      let newArr = []
      for (let i = 0; i < val.length; i++) {
        if (
          val.length - i >= 3 &&
          (val.length - i) % 3 === 0 &&
          newArr.length > 0
        ) {
          newArr.push(',')
        }
        newArr.push(val[i])
      }
      val = newArr.join('')
    }
    if (isNegative) return '- ' + val + decimal
    else return val + decimal
  }
}
